package com.yanggeng.demo.beginnerguidedemo.activity.customview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.yanggeng.demo.beginnerguidedemo.utils.DeviceParams;
import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.activity.BaseActivity;
import com.yanggeng.demo.beginnerguidedemo.bean.MileageStatisticsBean;
import com.yanggeng.demo.beginnerguidedemo.utils.SnackbarUtil;
import com.yanggeng.demo.beginnerguidedemo.widget.ScrollLineChartView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by yanggeng on 2017/6/30.
 */

public class LineChartActivity extends BaseActivity {

    private LineChart lineChart;
    private ScrollLineChartView scrollLineChartView;
    private HorizontalScrollView scrollView;
    private Button btnChange;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart);
        setTitle(true, "折线图", R.mipmap.c_home_setting_icon);

        scrollLineChartView = (ScrollLineChartView) findViewById(R.id.scroll_line_chart);
        btnChange = (Button) findViewById(R.id.btn_change_max);

        List<MileageStatisticsBean> data = new ArrayList<>();
        data.add(new MileageStatisticsBean("2017.12.04", 20.1f));
        data.add(new MileageStatisticsBean("2017.12.05", 8f));
        data.add(new MileageStatisticsBean("2017.12.06", 0f));
        data.add(new MileageStatisticsBean("2017.12.07", 10.1f));
        data.add(new MileageStatisticsBean("2017.12.08", 18.6f));
        data.add(new MileageStatisticsBean("2017.12.09", 34.5f));
        data.add(new MileageStatisticsBean("2017.12.10", 50.1f));
        data.add(new MileageStatisticsBean("2017.12.11", 10.1f));
        data.add(new MileageStatisticsBean("2017.12.12", 30.1f));
        data.add(new MileageStatisticsBean("2017.12.13", 20.1f));
        scrollLineChartView.setData(data);
        scrollLineChartView.setOnItemClickListener(new ScrollLineChartView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
//                btnChange.setText(data2.get(position).toString());
            }
        });
        final Random random = new Random();
        btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollLineChartView.setMaxVisibleValueCount(random.nextInt(10));
//                scrollLineChartView.setSelectItem(random.nextInt(data2.size()));
            }
        });

        lineChart = (LineChart) findViewById(R.id.lc_consume);

        lineChart.getLayoutParams().height = DeviceParams.screenWidth(this) / 2;

        setLineChart();
    }

    private String[] xData = {"2017.04", "2017.05", "2017.06", "2017.07", "2017.08"};

    private void setLineChart() {
        lineChart.setDrawGridBackground(false); //设置不显示图表网格背景
        lineChart.setDrawBorders(false); //设置不绘制图表边框线
        lineChart.setDescription(null);
        lineChart.getLegend().setEnabled(false);
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setEnabled(true); //设置启用图表x轴
        xAxis.setDrawAxisLine(false); //是否绘制x坐标轴
        xAxis.setDrawGridLines(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(10f);
        xAxis.setTextColor(Color.parseColor("#2b88e5"));
        xAxis.setAvoidFirstLastClipping(true); //图表将避免第一个和最后一个标签条目被减掉在图表或屏幕的边缘
        xAxis.setLabelCount(6, true);

        lineChart.getAxisRight().setEnabled(false);

        YAxis yAxis = lineChart.getAxisLeft();
        yAxis.setDrawAxisLine(false);
        yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        yAxis.setDrawGridLines(false);
        yAxis.setDrawLabels(false);
        yAxis.setTextColor(Color.WHITE);
        yAxis.setTextSize(14);
        yAxis.setXOffset(35f);

        lineChart.setTouchEnabled(false); //设置不可触摸
        lineChart.setScaleEnabled(false); //设置不可缩放
        lineChart.setDragEnabled(false); //设置不可拖拽
        lineChart.setDoubleTapToZoomEnabled(false); //设置不可双击放大

        lineChart.animateXY(2000, 2000);

        setLineChartData();
    }


    private void setLineChartData() {
        ArrayList<Entry> values = new ArrayList<>();
        values.add(new Entry(0, 0));
        values.add(new Entry(1, 50));
        values.add(new Entry(2, 20));
        values.add(new Entry(3, 0));
        values.add(new Entry(4, 80));
        values.add(new Entry(5, 40));
        LineDataSet set = new LineDataSet(values, "");
        set.setCircleColor(Color.parseColor("#2b88e5"));
        set.setCircleHoleRadius(5f);
        set.setColor(Color.parseColor("#2b88e5"));
        set.setMode(LineDataSet.Mode.LINEAR);
        set.setLineWidth(3f);
        set.setValueTextSize(16f);
        set.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                return new DecimalFormat("#.#").format(value);
            }
        });
        set.setValueTextColor(Color.parseColor("#2b88e5"));
        ArrayList<ILineDataSet> data = new ArrayList<>();
        data.add(set);
        lineChart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if (value == 0f) {
                    return "";
                } else {
                    return xData[(int) value - 1];
                }
            }
        });
        lineChart.setData(new LineData(data));
        lineChart.invalidate();
    }

    @Override
    protected void navigationOnclick(int paramId) {
        if(paramId == RIGHT_BUTTON) {
            SnackbarUtil.longSnackbar(main_view, "点击了设置");
        }
    }
}
